﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ServerChoosePanel : BasePanel
{
    public Transform leftContent;
    public Transform rightContent;
    
    public TMP_Text lastLogin_id;
    public TMP_Text lastLogin_name;
    public Image lastLogin_state;
    public TMP_Text ServerInfo;

    private ServerData serverData;

    [Header("状态图片")]
    public Sprite[] spriteState;
    public override void Init()
    {
        InitLastLoginInfo();

        InitLeftView();

        InitRightView(1, LoginManager.Instance.serverData.grounpInside);
    }

    /// <summary>
    /// 初始化左滚动视图卡片
    /// </summary>
    public void InitLeftView()
    {
        serverData = LoginManager.Instance.serverData;
        int start = 1;
        int end = 1;
        foreach (var item in serverData.servers)
        {
            if (end % serverData.grounpInside == 0)
            {
                ChooseCard card = Instantiate(Resources.Load<GameObject>("UI/ServerCard/ChooseCard"), leftContent).GetComponent<ChooseCard>();
                card.choosePanel = this;
                card.Init(start, end);
                start = end + 1;
            }
            end++;
        }
        int residueCard = serverData.servers.Count % serverData.grounpInside;
        if (residueCard != 0)
        {
            ChooseCard card = Instantiate(Resources.Load<GameObject>("UI/ServerCard/ChooseCard"), leftContent).GetComponent<ChooseCard>();
            card.choosePanel = this;
            card.Init(start, start + residueCard - 1);
        }
    }
    /// <summary>
    /// 清空右视图列表
    /// </summary>
    private void ClearRight()
    {
        for (int i = 0; i < rightContent.childCount; i++)
        {
            Destroy(rightContent.GetChild(i).gameObject);
        }
    }
    /// <summary>
    /// 初始化右视图列表
    /// </summary>
    /// <param name="start">初始服务器开始id</param>
    /// <param name="end">初始服务器结束id</param>
    public void InitRightView(int start, int end)
    {
        ClearRight();
        ServerInfo.text = start + "-" + end + "区";
        List<Server> servers = LoginManager.Instance.QueryServersById(start, end);
        foreach (Server server in servers)
        {
            ServerCard serverCard = Instantiate(Resources.Load<GameObject>("UI/ServerCard/ServerCard"), rightContent).GetComponent<ServerCard>();
            serverCard.choosePanel = this;
            serverCard.Init(server, spriteState);
        }
    }

    /// <summary>
    /// 初始化上次登录
    /// </summary>
    private void InitLastLoginInfo()
    {
        Server server = LoginManager.Instance.QueryServerById(LoginManager.Instance.loginData.serverId);
        lastLogin_id.text = server.id + "区";
        lastLogin_name.text = server.name;
        lastLogin_state.sprite = server.serverState switch
        {
            0 => spriteState[0],
            1 => spriteState[1],
            2 => spriteState[2],
            3 => spriteState[3],
            _ => null,
        };
    }

}
